﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Todo.Helpers
{
    public static class HttpClientHelper
    {
        public static HttpClient CreateHttpClient(string token)
        {
            HttpClient client = new HttpClient();
            // 配置请求的基地址，URL
            client.BaseAddress = new Uri(ConfigurationManager.AppSettings["BaseURL"]!);
            // 配置请求头
            // 先清空Accept头
            //client.DefaultRequestHeaders.Accept.Clear();
            //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));

            // 添加Bearer Token到请求头
            if (!string.IsNullOrEmpty(token))
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            return client;
        }
    }
}
